﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CalcEngine
{
    public abstract class CodeBuilder
    {
        private StringBuilder stringBuilder;
        public CodeBuilder()
            : this(new StringBuilder())
        {
        }
        public CodeBuilder(StringBuilder stringBuilder)
        {
            this.stringBuilder = stringBuilder;
        }
        public override string ToString()
        {
            if (IsWriting)
            {
                EndWrite();
            }
            return stringBuilder.ToString();
        }
        protected virtual void StartWriteCode()
        {
            IsWriting = true;

        }
        internal abstract void EndWrite();
        internal bool IsWriting;
        protected void Write(string s)
        {
            if (!IsWriting)
            {
                StartWriteCode();
            }
            stringBuilder.Append(s);
        }
        protected abstract void WriteIf(List<ICodeBuilder> list);

        internal virtual void Write(FunctionDefinition functionDefinition, List<ICodeBuilder> list)
        {
            switch (functionDefinition.Function.Method.Name.ToUpper())
            {
                case "IF":
                    WriteIf(list);
                    break;
                default:
                    throw new ArgumentException("Bad expression.");
            }

        }
    }
}
